Thread: Problem opening file

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    180

    Problem opening file

    It gives me error message that i used. Its first time i use const char *file, what is wrong? I have file in same folder as program.

    Code:
    int readStudentsFile(const char *file, Student Table[], int N)
    {
    	int i;
    	Student newStudent;
    	FILE *f;
    	f = fopen(file, "r");
    	if(f == NULL)
    	{
    		return -1; // error
    	}
    	while(!feof(f))
    	{
    		fscanf(f, "%d\n", &newStudent.num);
    }
    fclose(f);
    return 1;
    
    int main()
    {
    	int k, Size=50, num, pos, fileIsOkay;
    	char file[20];
    	Student *newStudent;
            HASH *HashTable;
    
    
    	
    	
    	
    	printf("Read file? (0 = no, 1 = yes) ");
    	scanf("%d", &k);
    	if(k==1)
    	{
    		fileIsOkay = readStudentsFile("data.txt", HashTable, Size);
    		if(fileIsOkay == -1)
    		{
    			printf("Error opening file \n");
    		}
    	}

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    If you'd use the autoformating - you'd see the missing brackets
    Code:
    int readStudentsFile(const char *file, Student Table[], int N)
    {
        int i;
        Student newStudent;
        FILE *f;
        f = fopen(file, "r");
        if(f == NULL)
        {
            return -1; // error
        }
        while(!feof(f))
        {
            fscanf(f, "%d\n", &newStudent.num);
        }
        fclose(f);
        return 1;
    
        int main()
        {
            int k, Size=50, num, pos, fileIsOkay;
            char file[20];
            Student *newStudent;
            HASH *HashTable;
    
    
    
    
    
            printf("Read file? (0 = no, 1 = yes) ");
            scanf("%d", &k);
            if(k==1)
            {
                fileIsOkay = readStudentsFile("data.txt", HashTable, Size);
                if(fileIsOkay == -1)
                {
                    printf("Error opening file \n");
                }
            }
    As a side note FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    Hate when i do that. That was error from translating the code. Is the code technically correct, like of you are going to use const char *file in parameter of function you pass file like that? And then if you it on the folder it should open with no problem, right? For some reason it's not working.

    EDIT: ok so i just tested with my usual process of specicying the directory inside the function and it still doesn't work. Always worked for me. What can be going on here? I can only see it as a permission thing or something but i never had problem in this directory.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you're having problems opening the file then instead of using printf() to print the error message try perror() instead, but do it where the failure occurred not in the calling function.

    Also depending on how you're running the program the working directory where the file should be located may be different.

    ok so i just tested with my usual process of specicying the directory inside the function and it still doesn't work.
    Since I have no idea what your "usual process" is this really means nothing. Show the code showing what you've tried.

    Jim

  5. #5
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    Yeah you're right should have provided some code. Anyways i tried what you suggested above, using perror where the error occurs. It gives me "No such file or directory"

  6. #6
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    Ok dumb mistake. Was using .txt in the filename in the directory and obviously was having troubles locating it since it was looking for data.txt and and not data.txt.txt

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Looks like you need to turn of that hide known extensions "feature" in your editor/explorer so it isn't so helpful. You'll have to find and read the documentation for the tools you're using to see how to turn of that "feature", or perhaps someone that does Windows might be able to tell you how.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem while opening a file !
    By rajarshi in forum C Programming
    Replies: 5
    Last Post: 11-06-2011, 08:33 AM
  2. problem in opening a txt file
    By arian in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2009, 01:17 PM
  3. file opening problem
    By bigmac(rexdale) in forum C++ Programming
    Replies: 17
    Last Post: 05-07-2008, 11:57 PM
  4. Problem with opening file and pathname
    By stevespai in forum C# Programming
    Replies: 3
    Last Post: 07-13-2006, 08:03 AM
  5. problem re-opening file
    By Markallen85 in forum C Programming
    Replies: 3
    Last Post: 06-12-2003, 11:02 AM